package mongo.db.clients.firsts;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Callable;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class NetflowGenIPList implements Callable<Integer> {

	public static void main(String[] args) throws InterruptedException, IOException {
		
		String ips = readFile("/home/murali/Documents/ipfile1.txt");
		int ipLength = ips.split(",").length;
		String[] ipArray = ips.split(",");
		System.out.println(ipLength);
		
		MongoClient	client = new MongoClient("119.226.225.253", 27017);
		
		DB db = client.getDB("netflowdb");
		DBCollection collection = db.getCollection("generated_netflow");
			Random rn = new Random();

		while(true ) {
				for (int i = 0; i < ipArray.length; i++) {
					collection.insert(new BasicDBObject()
							.append("routerip", ipArray[i])
//							.append("routerid", j)
							.append("sequenceid", 9999999)
							.append("srcadr", "202.144.74." + rn.nextInt(255))
							.append("dstadr", "202.144.74." + rn.nextInt(255))
							.append("inputint", rn.nextInt(5))
							.append("outputint", rn.nextInt(5))
							.append("octets", rn.nextInt(1000))
							.append("srcport", rn.nextInt(25))
							.append("dstport", rn.nextInt(25))
							.append("protocl", 6)
//							.append("tos", "0")
//							.append("dstas", "0")
							.append("direction", rn.nextInt(1))
//							.append("appid", "13")
							.append("timestamp", new Date()));
//				Thread.sleep( rn.nextInt(5) * 1000);
//				Thread.sleep( 100);
				}
		}
	}
	

	private static String readFile( String file ) throws IOException {
	    StringBuilder stringBuilder = null;
		try {
			BufferedReader reader = new BufferedReader( new FileReader (file));
			String         line = null;
			stringBuilder = new StringBuilder();
			String         ls = System.getProperty("line.separator");

			while( ( line = reader.readLine() ) != null ) {
			    stringBuilder.append( line );
			    stringBuilder.append( ls );
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	    return stringBuilder.toString();
	}


	@Override
	public Integer call() throws Exception {
		
		System.out.println("started...");
		String ips = readFile("/home/murali/Documents/ipfile1.txt");
		int ipLength = ips.split(",").length;
		String[] ipArray = ips.split(",");
		System.out.println(ipLength);
		
		MongoClient	client = new MongoClient("119.226.225.253", 27017);
		
		DB db = client.getDB("netflowdb");
		DBCollection collection = db.getCollection("generated_netflow");
			Random rn = new Random();

		while(true ) {
				for (int i = 0; i < ipArray.length; i++) {
					collection.insert(new BasicDBObject()
							.append("routerip", ipArray[i])
//							.append("routerid", j)
							.append("sequenceid", 9999999)
							.append("srcadr", "202.144.74." + rn.nextInt(255))
							.append("dstadr", "202.144.74." + rn.nextInt(255))
							.append("inputint", rn.nextInt(5))
							.append("outputint", rn.nextInt(5))
							.append("octets", rn.nextInt(1000))
							.append("srcport", rn.nextInt(25))
							.append("dstport", rn.nextInt(25))
							.append("protocl", 6)
//							.append("tos", "0")
//							.append("dstas", "0")
							.append("direction", rn.nextInt(1))
//							.append("appid", "13")
							.append("timestamp", new Date()));
//				Thread.sleep( rn.nextInt(5) * 1000);
//				Thread.sleep( 100);
				}
		}
		
	}

}
